"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : ALTER PROCEDURE
Case Name   : 非存储过程所有者普通用户被赋予存储过程alter权限后修改存储过程
              所有者
Create At   : 2022/05/10
Owner       : @zou_jialiang050
Description :
    1.创建用户1
    2.创建用户2
    3.创建用户3
    4.用户2的权限赋给用户1
    5.用户1所在模式的create权限赋予用户2
    6.用户1创建procedure
    7.对用户3赋权用户1模式的所有权限以及创建procedure的alter权限
    8.用户3修改procedure的所有者为用户2
    9.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.成功
    7.成功
    8.合理报错
    9.成功
History     :
"""

import os
import unittest
from yat.test import macro
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class Procedure(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.Con = Constant()
        self.user1 = 'u_alter_procedure_0023_01'
        self.user2 = 'u_alter_procedure_0023_02'
        self.user3 = 'u_alter_procedure_0023_03'
        self.procedure = 'p_alter_procedure_0023'

    def test_alter_procedure(self):
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        step = 'step1:创建用户1 expect:成功'
        self.logger.info(step)
        create_user = self.primary_sh.execut_db_sql(f'''
            drop user if exists {self.user1} cascade;
            create user {self.user1} with password '{macro.PASSWD_REPLACE}';
            ''')
        self.logger.info(create_user)
        self.assertIn(self.Con.CREATE_ROLE_SUCCESS_MSG, create_user,
                      '执行失败' + step)

        step = 'step2:创建用户2 expect:成功'
        self.logger.info(step)
        create_user = self.primary_sh.execut_db_sql(f'''
            drop user if exists {self.user2} cascade;
            create user {self.user2} with password '{macro.PASSWD_REPLACE}';
            ''')
        self.logger.info(create_user)
        self.assertIn(self.Con.CREATE_ROLE_SUCCESS_MSG, create_user,
                      '执行失败' + step)

        step = 'step3:创建用户3 expect:成功'
        self.logger.info(step)
        create_user = self.primary_sh.execut_db_sql(f'''
            drop user if exists {self.user3} cascade;
            create user {self.user3} with password '{macro.PASSWD_REPLACE}';
            ''')
        self.logger.info(create_user)
        self.assertIn(self.Con.CREATE_ROLE_SUCCESS_MSG, create_user,
                      '执行失败' + step)

        step = 'step4:将用户2的权限赋予用户1 expect:成功'
        self.logger.info(step)
        grant_pri = self.primary_sh.execut_db_sql(
            f'''grant {self.user2} to {self.user1};''')
        self.logger.info(grant_pri)
        self.assertIn('GRANT', grant_pri, '执行失败' + step)

        step = 'step5:用户1所在模式的所有权限赋予用户2 expect:成功'
        self.logger.info(step)
        grant_pri = self.primary_sh.execut_db_sql(
            f'''grant all privileges on schema {self.user1} to {self.user2};
            ''')
        self.logger.info(grant_pri)
        self.assertIn('GRANT', grant_pri, '执行失败' + step)

        step = 'step6:用户1创建procedure expect:成功'
        self.logger.info(step)
        create_pro = self.primary_sh.execut_db_sql(
            f'''create or replace procedure {self.procedure}()
            is
            var1 int:= 0;
            begin
                var1 := (select 1+1);
            end;''', sql_type=f" -U {self.user1} -W '{macro.PASSWD_REPLACE}'")
        self.logger.info(create_pro)
        self.assertIn(self.Con.CREATE_PROCEDURE_SUCCESS_MSG, create_pro,
                      '执行失败' + step)

        step = 'step7:对用户3赋权用户1模式的所有权限以及创建procedure的alter' \
               '权限 expect:成功'
        self.logger.info(step)
        grant_pri = self.primary_sh.execut_db_sql(
            f'''set current_schema = {self.user1};
            grant alter on procedure {self.procedure}() to {self.user3};
            grant all privileges on schema {self.user1} to {self.user3};''')
        self.logger.info(grant_pri)
        self.assertIn('SET', grant_pri, '执行失败' + step)
        self.assertEqual(grant_pri.count('GRANT'), 2, '执行失败' + step)

        step = 'step8:用户3修改procedure的所有者为用户2 expect:合理报错'
        self.logger.info(step)
        alter_procedure = self.primary_sh.execut_db_sql(
            f'''alter procedure {self.user1}.{self.procedure}() owner to 
            {self.user2};''', sql_type=f" -U {self.user3} -W "
            f"'{macro.PASSWD_REPLACE}'")
        self.logger.info(alter_procedure)
        self.assertIn(f'ERROR:  must be owner of function {self.procedure}',
                      alter_procedure, '执行失败' + step)

    def tearDown(self):
        step = 'step10:清理环境 expect:清理环境成功'
        self.logger.info(step)
        clean_environment = self.primary_sh.execut_db_sql(
            f'''drop user {self.user1} cascade;
            drop user {self.user2} cascade;
            drop user {self.user3} cascade;''')
        self.logger.info(clean_environment)
        self.assertEqual(clean_environment.count
                         (self.Con.DROP_ROLE_SUCCESS_MSG), 3,
                         '执行失败' + step)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
